﻿using BusinessEntity;
using BusinessEntity.xmglQuerryEntity;
using BusinessRepository;
using BusinessRule.Organization;
using learun.util;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Text;
using System.Threading.Tasks;

namespace BusinessRule
{
    public class ProYongZhangRule
    {
        #region 导出
        /// <summary>
        /// 用户列表（导出Excel）
        /// </summary>
        /// <returns></returns>
        public static async Task<MemoryStream> GetExportList(List<string> ids, List<ColumnModel> cols)
        {
            //List<string> ids = new List<string>();
            //List<ColumnModel> cols = new List<ColumnModel>();

            //取出数据源
            DataTable exportTable = await GetExportList(ids);
            //设置导出格式
            ExcelConfig excelconfig = new ExcelConfig();
            excelconfig.Title = "用章列表";
            excelconfig.TitleFont = "微软雅黑";
            excelconfig.TitlePoint = 25;
            //excelconfig.HeadPoint = 12;
            excelconfig.FileName = "用章列表.xls";
            excelconfig.IsAllSizeColumn = true;
            //每一列的设置,没有设置的列信息，系统将按datatable中的列名导出
            excelconfig.ColumnEntity = new List<ColumnModel>();
            for (int i = 0; i < cols.Count; i++)
            {
                excelconfig.ColumnEntity.Add(new ColumnModel() { Column = cols[i].Column, ExcelColumn = cols[i].ExcelColumn });
            }

            return ExcelHelper.ExportMemoryStream(exportTable, excelconfig);
        }

        public async static Task<DataTable> GetExportList(List<string> ids)
        {
            //throw new NotImplementedException();
            string yids = string.Join("','", ids);
            yids = "'" + yids + "'";
            string strsql = "select * from View_ProHTYZLists where id in (" + yids + ")";
            return await new RepositoryFactory().BaseRepository().FindTable(strsql);
        }

        #endregion


        #region 用章

        public async static Task<(IEnumerable<dynamic>, int)> GetHTSealIndexList(dynamic dye, string strdepids)
        {
            string stryzlx = dye.yzlx;
            string yzmc = dye.yzmc;
            string yzwj = dye.yzwj;
            string yzbm = dye.yzbm;
            string yzr = dye.yzr;
            //string yearRange = dye.yearRange;

            string kssj = dye.kssj;
            string jssj = dye.jssj;

            //dye.kssj = kssj;
            //dye.jssj = jssj;

            //dye.yzmc = yzmc;
            //dye.yzwj = yzwj;
            //dye.yzbm = yzbm;
            //dye.yzr = yzr;
            //dye.yearRange = yearRange;

            string status = dye.status;
            string current = dye.current;
            string size = dye.size;
            string sort = dye.sort ?? "";
            string column = dye.column ?? "";

            string str = "";
            if (!(string.IsNullOrEmpty(stryzlx)))
            {
                str = str + " and yzlx = '" + stryzlx + "'";
            }

            if (!(string.IsNullOrEmpty(yzmc)))
            {
                str = str + " and mc like '%" + yzmc + "%'";
            }
            if (!(string.IsNullOrEmpty(yzwj)))
            {
                str = str + " and yzwjmc like '%" + yzwj + "%'";
            }
            if (!(string.IsNullOrEmpty(yzbm)))
            {
                str = str + " and bmid = '" + yzbm + "'";
            }
            if (!(string.IsNullOrEmpty(yzr)))
            {
                str = str + " and yzr = '" + yzr + "'";
            }

            if (!(string.IsNullOrEmpty(kssj)))
            {
                str = str + " and yzsj >= '" + kssj + "'";
            }

            if (!(string.IsNullOrEmpty(jssj)))
            {
                str = str + " and yzsj <= '" + jssj + "'";
            }

            //if (!(string.IsNullOrEmpty(yearRange)))
            //{
            //    string[] strs = yearRange.Split('_');

            //    str = str + " and yzsj >= '" + strs[0].Split('-')[0] + "'";

            //    str = str + " and yzsj <= '" + strs[1].Split('-')[0] + "'";
            //}

            if (!(string.IsNullOrEmpty(status)))
            {
                //str += status == "1" ? " and jusstatus= '决算'" : " and jusstatus= '未决算'";

                str += " and status = '"+ status + "'";
            }



            //await UserRule.GetUserDepid(uid)
            string sql = "select * from View_ProHTYZLists where bmid in " + strdepids + str.Replace("''", "'");
            bool isAsc = true;
            if (sort != "Asc" && sort != "asc")
            {
                isAsc = false;
            }
            if (string.IsNullOrEmpty(column))
            {
                column = "id";
            }
            return await new RepositoryFactory().BaseRepository().FindList(sql, null, column, isAsc, size.ToInt(), current.ToInt());

        }


        public async static Task<DataTable> GetHTSealList(string htid,string uid)
        {
            string strwhere = "";
            if (!string.IsNullOrEmpty(htid))
            {
                strwhere+=" and htid = '"+ htid + "'";
            }
            string strsql = "select * from View_ProHTYZLists where 1=1 "+ strwhere + " and bmid in " + await UserRule.GetUserDepid(uid);
 
            return await new RepositoryFactory().BaseRepository().FindTable(strsql);
        }


        public async static Task<DataTable> GetHTSealByid(string logid)
        {
            string strsql = "select * from View_ProHTYZLists where id = '" + logid + "'";

            return await new RepositoryFactory().BaseRepository().FindTable(strsql);

        }

        public async static Task<int> InsertHTSeal(CProYongZhang log, string ope)
        {
            if (ope == "add")
            {
                return await new RepositoryFactory().BaseRepository().Insert(log);
            }
            else// (ope == "edit")
            {
                return await new RepositoryFactory().BaseRepository().Update(log);
            }

        }

        public async static Task<bool> DeleteHTSeal(Root ids)
        {
            return await TranstionRule.GetRunActionTransAsync(async irp =>
            {
                foreach (BaseDeleteMulti idm in ids.ids)
                {
                    await irp.DeleteAny<CProYongZhang>(new { id = idm.id });


                }
                return true;
            });

            //return await new RepositoryFactory().BaseRepository().DeleteAny<CProConBG>(new { id = bgid });
        }
        #endregion
    }
}
